// 定义消息类型枚举
enum MessageType {
    Server = 'server',
    User = 'user'
}

// 基础消息类
class Message {
    id: string;
    timestamp: Date;
    content: string;
    type: MessageType;

    constructor(id: string, timestamp: Date, content: string, type: MessageType) {
        this.id = id;
        this.timestamp = timestamp;
        this.content = content;
        this.type = type;
    }
}

// 服务器消息类
class ServerMessage extends Message {
    level: 'info' | 'warning' | 'error';

    constructor(id: string, timestamp: Date, content: string, level: 'info' | 'warning' | 'error') {
        super(id, timestamp, content, MessageType.Server);
        this.level = level;
    }
}

// 用户消息类
class UserMessage extends Message {
    role: 'admin' | 'moderator' | 'player';

    constructor(id: string, timestamp: Date, content: string, role: 'admin' | 'moderator' | 'player') {
        super(id, timestamp, content, MessageType.User);
        this.role = role;
    }
}

// 类型守卫函数
function isServerMessage(message: Message): message is ServerMessage {
    return message.type === MessageType.Server;
}

function isUserMessage(message: Message): message is UserMessage {
    return message.type === MessageType.User;
}

// 示例用法
const messages: Message[] = [
    new ServerMessage('1', new Date(), '服务器启动成功', 'info'),
    new UserMessage('2', new Date(), '玩家加入游戏', 'player')
];

messages.forEach(message => {
    if (isServerMessage(message)) {
        console.log(`服务器消息: ${message.content}, 等级: ${message.level}`);
    } else if (isUserMessage(message)) {
        console.log(`用户消息: ${message.content}, 角色: ${message.role}`);
    } else {
        console.log(`未知消息类型: ${message.content}`);
    }
});